package com.android.vgongyi.activity;

import sina.SinaBlogException;
import sina.SinaRequestListener;
import sohu.SohuBlogException;
import sohu.SohuRequestListener;
import tencent.utils.TencentException;
import tencent.utils.TencentRequestListener;
import wangyi.TBlogException;
import wangyi.WangyiRequestListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.vgongyi.R;
import com.android.vgongyi.common.AppApiPreference;
import com.android.vgongyi.common.AppConstant;
import com.android.vgongyi.service.BlogPreferenceService;
import com.android.vgongyi.service.VGongyiService;
import com.android.vgongyi.view.TransferAuthDialog;
/**
 * 快速回复
 * @author Ryan
 *
 */
public class ReplyActivity extends BaseActivity implements OnClickListener{
	private EditText replyContentEditText;
	private String weiboID;
	private String weiboPlatform;
	private String weiboComment;
	private VGongyiService service;
	
	private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
		
			if(msg.what == AppConstant.NO_0){
				Toast.makeText(getApplicationContext(), msg.obj.toString(),Toast.LENGTH_SHORT).show();
			}
			if(msg.what == AppConstant.NO_1){
				findViewById(R.id.title_progress).setVisibility(View.GONE);
				findViewById(R.id.Common_refresh).setVisibility(View.VISIBLE);
				finish();
			}
			if(msg.what == AppConstant.NO_3){
				findViewById(R.id.title_progress).setVisibility(View.GONE);
				findViewById(R.id.Common_refresh).setVisibility(View.VISIBLE);
			}
			
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reply);
        setCommonTitle(this, R.string.reply_title);
        
        Intent intent = getIntent();
        weiboID       = intent.getStringExtra("weiboID");
        weiboPlatform = intent.getStringExtra("weiboPlatform");
        
        service = new VGongyiService(this);
        final TextView textCountTextView = (TextView) this.findViewById(R.id.text_count);
        replyContentEditText             = (EditText) this.findViewById(R.id.reply_content);
        Button backBtn                   = (Button) this.findViewById(R.id.Common_back);
        Button replyBtn                  = (Button) this.findViewById(R.id.Common_refresh);
        
        backBtn.setVisibility(View.VISIBLE);
        replyBtn.setText(R.string.reply_okbtn);
        replyBtn.setVisibility(View.VISIBLE);
        
        replyContentEditText.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				textCountTextView.setText(String.valueOf(140 - replyContentEditText.getText().length()));
				
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
			
			public void afterTextChanged(Editable s) {}
		});
        
        backBtn.setOnClickListener(this);
        replyBtn.setOnClickListener(this);
        
	}
	
	@Override
	protected void onResume() {
		findViewById(R.id.title_progress).setVisibility(View.GONE);
		findViewById(R.id.Common_refresh).setVisibility(View.VISIBLE);
		super.onResume();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Common_back:
			ReplyActivity.this.finish();
			break;
		case R.id.Common_refresh://快速回复

			BlogPreferenceService preference = new BlogPreferenceService(getApplicationContext());
			weiboComment = replyContentEditText.getText().toString().trim();
			
			if(TextUtils.isEmpty(weiboComment)){
				sendHandlerMessage("给回复内容加点料吧!");
				return;
			}
			
			
			findViewById(R.id.title_progress).setVisibility(View.VISIBLE);
			v.setVisibility(View.GONE);
			if(weiboPlatform.equals(AppApiPreference.weiboPlatform[0])){
				if(!preference.isSinaBlogAuthorized()){
					new TransferAuthDialog(ReplyActivity.this).show();
					mHandler.sendEmptyMessage(AppConstant.NO_3);
					return;
				}
				service.sendCommentToSina(ReplyActivity.this, weiboID, weiboComment, sinaRequestListener);
				
			}else if(weiboPlatform.equals(AppApiPreference.weiboPlatform[1])){
				if(!preference.isTencentBlogAuthorized()){
					new TransferAuthDialog(ReplyActivity.this).show();
					mHandler.sendEmptyMessage(AppConstant.NO_3);
					return;
				}
				service.sendCommentToTecentWeibo(ReplyActivity.this, weiboID,weiboComment,tencentRequestListener );
				
			}else if(weiboPlatform.equals(AppApiPreference.weiboPlatform[2])){
				if(!preference.isSohuBlogAuthorized()){
					new TransferAuthDialog(ReplyActivity.this).show();
					mHandler.sendEmptyMessage(AppConstant.NO_3);
					return;
				}
				service.sendCommentToSohu(ReplyActivity.this, weiboID, weiboComment,sohuRequestListener);
				
			}else if(weiboPlatform.equals(AppApiPreference.weiboPlatform[3])){
				if(!preference.isWangyiBlogAuthorized()){
					new TransferAuthDialog(ReplyActivity.this).show();
					mHandler.sendEmptyMessage(AppConstant.NO_3);
					return;
				}
				service.sendCommentToWangyiWeibo(ReplyActivity.this, weiboID, weiboComment,wangyiRequestListener);
				
			}
			mHandler.sendEmptyMessage(AppConstant.NO_1);
			break;
		}
	}
	
	private void sendHandlerMessage(String message){
		Message msg = new Message();
		msg.what = AppConstant.NO_0;
		msg.obj  = message;
		mHandler.sendMessage(msg);
	}
	
	private SinaRequestListener sinaRequestListener = new SinaRequestListener() {
		
		@Override
		public void onRequestStart() {
			
		}
		
		@Override
		public void onRequestFault(Throwable fault) {
			sendHandlerMessage("回复失败");
			Log.i("msg","exception sina = " + fault.getMessage());
		}
		
		@Override
		public void onRequestError(SinaBlogException exception) {
			sendHandlerMessage("回复失败");
			Log.i("msg","exception sina = " + exception.getMessage());
		}
		
		@Override
		public void onRequestComplete(String response) {
			sendHandlerMessage("回复成功");
		}
	};
	
	private TencentRequestListener tencentRequestListener =  new TencentRequestListener() {
		
		public void onRequestStart() {
			
		}
		
		public void onRequestFault(Throwable fault) {
			
			sendHandlerMessage("回复失败");
			Log.i("msg","exception tencent = " + fault.getMessage());
		}
		
		public void onRequestError(TencentException exception) {
			
			sendHandlerMessage("回复失败");
			Log.i("msg","exception tencent = " + exception.getMessage());
		}
		
		public void onRequestComplete(String response) {
			sendHandlerMessage("回复成功");
		}
	};
	
	private SohuRequestListener sohuRequestListener =  new SohuRequestListener() {

		public void onRequestStart() {
			
		}

		@Override
		public void onRequestError(SohuBlogException exception) {
			sendHandlerMessage("回复失败");
			Log.i("msg","exception sohu = " + exception.getMessage());
		}

		@Override
		public void onRequestFault(Throwable fault) {
			sendHandlerMessage("回复失败");
			Log.i("msg","exception sohu = " + fault.getMessage());
		}
		

		@Override
		public void onRequestComplete(String response) {
			sendHandlerMessage("回复成功");
		}
	};
	
	
	
	private WangyiRequestListener wangyiRequestListener  = new WangyiRequestListener() {
		
		public void onRequestStart() {
			
		}
		
		public void onRequestFault(Throwable fault) {
			sendHandlerMessage("回复失败");
			Log.i("msg","exception wangyi = " + fault.getMessage());
		}
		
		public void onRequestError(TBlogException exception) {
			sendHandlerMessage("回复失败");
			Log.i("msg","exception wangyi = " + exception.getMessage());
		}
		
		public void onRequestComplete(String response) {
			sendHandlerMessage("回复成功");
	      	  	  
		}
	};
}
